package net.huashitong.Project.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;
import net.huashitong.Project.dao.RelocateSummaryDao;
import net.huashitong.Project.entity.RelocateProgress;
import net.huashitong.Project.entity.RelocateSummary;
import net.huashitong.Project.filter.RelocateProgressFilter;
import net.huashitong.Project.filter.RelocateSummaryFilter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RelocateSummaryService extends StringPKService<RelocateSummary> {

    @Autowired
    private RelocateSummaryDao dao;
    @Autowired
    private SysTableService tableService;

    @Override
    protected BaseDao<RelocateSummary, String> getDao() {
        return dao;
    }
    @Override
    protected void beforeSave(RelocateSummary obj) {
        obj.setSysId(tableService.updateMaxSysId("t_bnp_relocate_summary", null));
    }

    public List<RelocateSummary> queryByProjectId(String projectId){
        return dao.queryByProjectId(projectId);
    }

    public void addRelocateSummary(RelocateSummaryFilter filter){
        RelocateSummaryFilter f = new RelocateSummaryFilter();
        f.setProjectId(f.getProjectId());
        List<RelocateSummary> list = dao.queryRelocateSummaryByFilter(f);
        if (list.size() > 0) {
            //如果有该时间的数据则更新，否则新增
            RelocateSummary relocateSummary = list.get(0);
            filter.setSysId(relocateSummary.getSysId());
            BeanUtils.copyProperties(filter, relocateSummary);
            update(relocateSummary);
        }else{
            RelocateSummary relocateSummary = new RelocateSummary();
            BeanUtils.copyProperties(filter, relocateSummary);
            this.save(relocateSummary);
        }
    }
}
